﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InfoStuffStore.Common;

namespace InfoStuffStore
{
    public partial class FormProductList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString.AllKeys.Contains("scatid") && Request.QueryString["scatid"] != "")
                {
                    LoadProductByCategory(Convert.ToInt32(Request.QueryString["scatid"]));
                }
                else if (Request.QueryString.AllKeys.Contains("catid") && Request.QueryString["catid"]!="")
                {
                    LoadProductByCategory(Convert.ToInt32(Request.QueryString["catid"]));
                }
                else if (Request.QueryString.AllKeys.Contains("brand"))
                {
                    LoadProductByBrand(Convert.ToInt32(Request.QueryString["brand"]));
                }
                else if (Request.QueryString.AllKeys.Contains("search"))
                {
                    SearchProducts(Request.QueryString["search"]);
                }
            }
        }

        private void SearchProducts(string searchText)
        {
            using (DBDataContext dataContext = new DBDataContext())
            {
                GridProductList.DataSource = dataContext.SearchProducts(searchText);
                GridProductList.DataBind();
            }
        }

        private void LoadProductByCategory(int categoryId)
        {
            int pageNo=1;
            int? totalRec = 0;
            if(Request.QueryString.AllKeys.Contains("page"))
            {
                pageNo=Convert.ToInt32(Request.QueryString["page"]);
            }

            using (DBDataContext dataContext = new DBDataContext())
            {
                if (pageNo == 1)
                {
                    GridProductList.DataSource = dataContext.GetProductsByCategory(categoryId, 0, 10, ref totalRec);
                }
                else
                {
                    GridProductList.DataSource = dataContext.GetProductsByCategory(categoryId, (pageNo * 10) + 1, 10, ref totalRec);
                }

                GridProductList.DataBind();
            }
        }

        private void LoadProductByBrand(int brandId)
        {
            int pageNo = 1;
            int? totalRec = 0;
            if (Request.QueryString.AllKeys.Contains("page"))
            {
                pageNo = Convert.ToInt32(Request.QueryString["page"]);
            }

            using (DBDataContext dataContext = new DBDataContext())
            {
                if (pageNo == 1)
                {
                    GridProductList.DataSource = dataContext.GetProductsByBrand(brandId, 0, 10, ref totalRec);
                }
                else
                {
                    GridProductList.DataSource = dataContext.GetProductsByBrand(brandId, (pageNo * 10) + 1, 10, ref totalRec);
                }

                GridProductList.DataBind();
            }
        }

        public string CategoryID
        {
            get
            {
                return Request.QueryString["catid"];
            }
        }
    }
}